using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shield.Web.UI;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public string SelectedQuarter
        {
            get
            {
                if (Session["SelectedQuarter"] == null)
                {
                    Session["SelectedQuarter"] = string.Empty;
                }

                return Session["SelectedQuarter"].ToString();
            }
            set
            {
                Session["SelectedQuarter"] = value;
            }
        }

        public List<PerformanceData> DataPerformance { get; set; }
        public List<SalesDataProduct> SalesData { get; set; }

        protected void ShieldChart1_TakeDataSource(object sender, Shield.Web.UI.ChartTakeDataSourceEventArgs e)
        {
            ShieldChart1.DataSource = new object[] 
            {
                new {Quarter = "Q1", Sales = 312 }, 
                new {Quarter = "Q2", Sales = 212 }, 
                new {Quarter = "Q3", Sales = 322 }, 
                new {Quarter = "Q4", Sales = 128 }
            };
        }

        protected void ShieldChart2_TakeDataSource(object sender, ChartTakeDataSourceEventArgs e)
        {
            ShieldChart2.DataSource = DataPerformance;
        }

        protected void ShieldChart3_TakeDataSource(object sender, ChartTakeDataSourceEventArgs e)
        {
            ShieldChart3.DataSource = SalesData;
        }

        protected void ShieldChart1_SelectionChanged(object sender, ChartSelectionChangedEventArgs e)
        {
            if (e.Selected)
            {
                SelectedQuarter = e.Name;
                DataPerformance = GetPerformanceData().Where(d => d.Quarter == SelectedQuarter).ToList();
            }
            else
            {
                DataPerformance = null;
            }
            ShieldChart2.DataBind();
        }

        protected void ShieldChart2_SelectionChanged(object sender, ChartSelectionChangedEventArgs e)
        {
            if (e.Selected)
            {
                SalesData = GetSalesDataProducts().Where(s => s.Quarter == SelectedQuarter && s.Product == e.Item.ValueX.ToString()).ToList();
            }
            else
            {
                SalesData = null;
            }
            ShieldChart3.DataBind();
        }

        private List<PerformanceData> GetPerformanceData()
        {
            return new List<PerformanceData>() { 
            new PerformanceData(){ Sales=123, Product="Women", Quarter="Q1", Data= 8.2 },
            new PerformanceData(){ Sales=123, Product="Men", Quarter="Q1", Data= 9.5 },
            new PerformanceData(){ Sales=123, Product="Baby", Quarter="Q1", Data= 5 },
            new PerformanceData(){ Sales=123, Product="Kids", Quarter="Q1", Data= 3.1 },
            new PerformanceData(){ Sales=123, Product="Home", Quarter="Q1", Data= 2.4 },
            new PerformanceData(){ Sales=123, Product="Furniture", Quarter="Q1", Data= 4.5 },
            new PerformanceData(){ Sales=123, Product="Electronics", Quarter="Q1", Data= 9.4 },
            new PerformanceData(){ Sales=123, Product="Entertainment", Quarter="Q1", Data= 8.4 },
            new PerformanceData(){ Sales=123, Product="Health&Beauty", Quarter="Q1", Data= 7.4 },
            new PerformanceData(){ Sales=123, Product="Clearance", Quarter="Q1", Data= 9.4 },

            new PerformanceData(){ Sales=121, Product="Women", Quarter="Q2", Data= 7.7 },
            new PerformanceData(){ Sales=121, Product="Men", Quarter="Q2", Data= 9.5 },
            new PerformanceData(){ Sales=121, Product="Baby", Quarter="Q2", Data= 9 },
            new PerformanceData(){ Sales=121, Product="Kids", Quarter="Q2", Data= 3.1 },
            new PerformanceData(){ Sales=121, Product="Home", Quarter="Q2", Data= 5.4 },
            new PerformanceData(){ Sales=121, Product="Furniture", Quarter="Q2", Data= 3.5 },
            new PerformanceData(){ Sales=121, Product="Electronics", Quarter="Q2", Data= 7.4 },
            new PerformanceData(){ Sales=121, Product="Entertainment", Quarter="Q2", Data= 8.4 },
            new PerformanceData(){ Sales=121, Product="Health&Beauty", Quarter="Q2", Data= 9.4 },
            new PerformanceData(){ Sales=121, Product="Clearance", Quarter="Q2", Data= 19.4 },

            new PerformanceData(){ Sales=144, Product="Women", Quarter="Q3", Data= 5.7 },
            new PerformanceData(){ Sales=144, Product="Men", Quarter="Q3", Data= 9.5 },
            new PerformanceData(){ Sales=144, Product="Baby", Quarter="Q3", Data= 5.7 },
            new PerformanceData(){ Sales=144, Product="Kids", Quarter="Q3", Data= 1.1 },
            new PerformanceData(){ Sales=144, Product="Home", Quarter="Q3", Data= 7.4 },
            new PerformanceData(){ Sales=144, Product="Furniture", Quarter="Q3", Data= 8.5 },
            new PerformanceData(){ Sales=144, Product="Electronics", Quarter="Q3", Data= 5.4 },
            new PerformanceData(){ Sales=144, Product="Entertainment", Quarter="Q3", Data= 8.4 },
            new PerformanceData(){ Sales=144, Product="Health&Beauty", Quarter="Q3", Data= 9.4 },
            new PerformanceData(){ Sales=144, Product="Clearance", Quarter="Q3", Data= 16.4 },

            new PerformanceData(){ Sales=254, Product="Women", Quarter="Q4", Data= 2.7 },
            new PerformanceData(){ Sales=254, Product="Men", Quarter="Q4", Data= 9.5 },
            new PerformanceData(){ Sales=254, Product="Baby", Quarter="Q4", Data= 5.9 },
            new PerformanceData(){ Sales=254, Product="Kids", Quarter="Q4", Data= 8.1 },
            new PerformanceData(){ Sales=254, Product="Home", Quarter="Q4", Data= 5.4 },
            new PerformanceData(){ Sales=254, Product="Furniture", Quarter="Q4", Data= 13.5 },
            new PerformanceData(){ Sales=254, Product="Electronics", Quarter="Q4", Data= 17.4 },
            new PerformanceData(){ Sales=254, Product="Entertainment", Quarter="Q4", Data= 18.4 },
            new PerformanceData(){ Sales=254, Product="Health&Beauty", Quarter="Q4", Data= 9.4 },
            new PerformanceData(){ Sales=254, Product="Clearance", Quarter="Q4", Data= 25.4 }
        };
        }

        private List<SalesDataProduct> GetSalesDataProducts()
        {
            List<SalesDataProduct> data = new List<SalesDataProduct>();
            List<string> products = new List<string>() { "Women", "Men", "Baby", "Kids", "Home", "Furniture", "Electronics", "Entertainment", "Health&Beauty", "Clearance" };
            for (int i = 1; i < 5; i++)
            {
                for (int x = 0; x < 10; x++)
                {
                    for (int j = 0; j < 12; j++)
                    {
                        SalesDataProduct item = new SalesDataProduct() { Quarter = "Q" + i.ToString(), Product = products[x], QuarterSales = new Random((i + j) * DateTime.Now.Second).Next(i * j, 100) };
                        data.Add(item);
                    }
                }
            }

            return data;
        }

        public class SalesDataProduct
        {
            public string Quarter { get; set; }
            public string Product { get; set; }
            public int QuarterSales { get; set; }
        }

        public class PerformanceData
        {
            public string Quarter { get; set; }
            public int Sales { get; set; }
            public string Product { get; set; }
            public double Data { get; set; }
        }
    }
}
